<? 
	if(isset($_POST['btngo']))
	{
		require("required/dbconnect.php");
		$pageno=$_POST['pageno'];
		$status=$_POST['status'];
		header("Location:member_viewall.php?page=$pageno&status=$status");
	}
	
	if(isset($_POST['btndelete']))
	{
		require("required/dbconnect.php");
		$deleteIds="";
		if(!empty($_POST['message_id']))
		{
			$postCount = count($_POST['message_id']);
			$msg="";
			
			
			if($postCount!=0)
			{
				for ( $i=0; $i < $postCount; $i++ ) 
				{
					$deleteIds .= $_POST['message_id'][$i].',';
				}
				
				$deleteIds = rtrim($deleteIds,',');
				
				
				mysql_query('delete from os_member where id in ('.$deleteIds.')');
			}
		}
		header("Location:member_viewall.php");
	}
	else
	{	
	$title="View all member"; 
	$pageid="1";
	$accessid="a00";
	
	require("required/dbconnect.php"); 
	require("required/function.php");
	require("required/header.php");
	
	checkAccess("allow_shopping_management",1);
	
	if(empty($_GET['page']))
	{
		$page="1";
	}
	else
	{
		$page=$_GET['page'];
	}
	
	$totalperpage=30;

	$from=($page-1)*$totalperpage;	
	if(isset($_POST['btnstatus']))
	{
		$status=$_POST['status'];
		if($status=="")
		{
			$status_q="";
			$schk="selected";
		}
		else
		{
			$status_q="and status='$status'";
			$schk="";
			$option="<option value='$status' selected>$status</option>";
		}
	}
	elseif(!empty($_GET['status']))
	{
		$status=$_GET['status'];
		
		if($status=="")
		{
			$status_q="";
			$schk="selected";
		}
		else
		{
			$status_q="and status='$status'";
			$schk="";
			$option="<option value='$status' selected>$status</option>";
		}
	
	}
	else
	{
		if(empty($_GET['status']))
		{
			$status="";
		 	$status_q="";
			$schk="selected";
		}
		else
		{
			$status=$_GET['status'];
			$status_q="and status='$status'";
			$schk="";
			$option="<option value='$status' selected>$status</option>";
		}
	}	
	
	
	$get_products=mysql_query("select * from os_member where (member_type='N' or member_type='M') $status_q order by id desc limit $from,$totalperpage")or die(mysql_error());
	$get_total=mysql_query("select * from os_member where (member_type='N' or member_type='M') $status_q")or die(mysql_error());
	
?>
<table width="100%">

<?
	$count=mysql_num_rows($get_total);

	$totalpage=$count/$totalperpage;
	
	if($count%$totalperpage>0)
	{
		if(round($totalpage)>$totalpage)
		{
			$totalpage=round($totalpage);
		}
		else
		{
			$totalpage=round($totalpage+1);
		}
	}
	else
	{
		$totalpage=round($totalpage);
	}
	
	
	$nextpage=$page+1;
	$prepage=$page-1;
	
	
?>
<SCRIPT LANGUAGE="JavaScript">
		function checkall(form, prefix, checkall) 
		{
			var checkall = checkall ? checkall : 'chkall';
			for(var i = 0; i < form.elements.length; i++) {
				var e = form.elements[i];
				if(e.name != checkall && (!prefix || (prefix && e.name.match(prefix)))) {
					e.checked = form.elements[checkall].checked;
				}
			}
		}
		
		function checkfields(frm)
		{
		if(confirm("Confirm to delete?"))
		return true;
		else
		return false;
		}
		
		//  End -->
	</script>
<td align="left" valign="top">
	<div class="title">View All Members</div>
	<table width="100%" cellpadding="2" cellspacing="1" >
	<tr>
		<td colspan="8">
		<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
		View 
		<select name="status" class="text">
			<?= $option ?>
			<option value="" <?= $schk ?>>All</option>
			<option value="activated">active</option>
			<option value="inactivate">inactivate</option>
		</select> members
		<input type="submit" value="Submit" name="btnstatus" class="button">
	
		</form>
		</td>
	</tr>
		<? if(mysql_num_rows($get_products)==0){ ?>
		<tr>
		 <td height="200" valign="middle" colspan="8" align="center">
			<font color="#FF0000">- No Record -</font><br>
		 </td>
		</tr>
		<? }else{ ?>
		<tr>
					<td colspan="10" align="left"><br><br>
						<table width="100%" cellpadding="0" cellspacing="0" >
							<td width="69%" height="45">
							<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
							<input type="button" class="button" value="<<" onClick="window.open('member_viewall.php?page=1&status=<?= $status ?>','_self')">
							<input type="button" class="button" value="<" onClick="window.open('member_viewall.php?page=<?= $prepage ?>&status=<?= $status ?>','_self')">
							<input type="hidden" value="<?= $status ?>" name="status">
							Page <input type="text" name="pageno" value="<?= $page ?>" size="2" maxlength="3" class="text"> of <?= $totalpage ?>
							<input type="submit" value="Go" class="button" name="btngo">
							<? if($page==$totalpage){ ?>	
							<input type="button" class="button" value=">" >
							<? }else{ ?>
							<input type="button" class="button" value=">" onClick="window.open('member_viewall.php?page=<?= $nextpage ?>&status=<?= $status ?>','_self')">
							<? } ?>
							<input type="button" class="button" value=">>" onClick="window.open('member_viewall.php?page=<?= $totalpage ?>&status=<?= $status ?>','_self')">
							
							</form>
							
							</td>
							
							
					  </table>
					</td>
				</tr>
				<tr>
					<td colspan="8"><?= mysql_num_rows($get_total) ?> records found</td>
				</tr>
				<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" name="frm">
			<tr height="20" align="center" class="table_header" >
			<Td width="32">No</Td><td width="4%"><input class="checkbox" type="checkbox" name="chkall" onclick="checkall(this.form)"></td><td width="222">Member name</td>
            <td width="100">Membership number</td><td width="100">Membership status</td>
            <td width="153">Email</td><td width="98">Registered time</td><td width="67">Action</td>
		</tr>
		<?
			$no=$from+1;
			while($a=mysql_fetch_array($get_products))
			{
				if($no%2==0)
				{
					$class1="table_item_even";
					$class2="table_item_odd";
				}
				else
				{
					$class1="table_item_odd";
					$class2="table_item_even";
				}
				
				if($a['membership_status']==0)
				{
					$membership_status="Not valid";
				}else{
					$membership_status="Valid";
				}
				
		?>	
		  <tr height="20" valign="top" class="<?= $class1 ?>"  onmouseover="this.className='table_item_mouseover'" 
                            onmouseout="this.className='<?= $class1 ?>'">
			<td><?= $no ?></td><td align="center"><input type="checkbox" name="message_id[]" value="<?= $a["id"] ?>"></td><Td><?= $a['name'] ?></td>
            <td><?= $a['membership_number'] ?></Td><td align="center"><?= $a['status'] ?></td><td><?= $a['email'] ?></td><td><?= $a['registerdate'] ?></td><td align="center"><a href="member_viewfull.php?id=<?= $a['id'] ?>"><img src="images/icons/view_icon.gif" border="0"></a></td>
		</Tr>
		
		<? $no++;} ?>
		<tr>
			<td colspan="4"><input type="submit" value="Delete" class="button" name="btndelete" onclick="return confirm('Confirm to delete?')"> </td>
		</tr>
		</form>
		<? } ?>
	</table>
	</td>
</table>

<? }require("required/footer.php"); ?>

